﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using System.Data;
using System.Web.Security;
using vncom.Models;

namespace vncom.Controllers
{
    public class ProductCatalogController : Controller
    {
        //
        // GET: /ProductCatalog/

        public ActionResult Index()
        {
            var dt = DataProvider.getByUserName(Membership.GetUser().UserName);
            List<ProductCatalogTree> DMTreeList = new List<ProductCatalogTree>();
            if (dt != null)
            {
                var Pram = setParam(dt.Rows[0]["Domain"].ToString(), "0");
                DataTable dtParent = DataProvider.SelectStoreProcedure("DM_SanPhamUser_Lst", Pram);
                if (dtParent.Rows.Count > 0)
                {
                    for (int i = 0;i<dtParent.Rows.Count;i++)
                    {
                        ProductCatalogTree DMTree = new ProductCatalogTree();
                        DMTree.DanhMucSanPhamID = dtParent.Rows[i]["DanhMucSanPhamID"].ToString();
                        DMTree.Seq = dtParent.Rows[i]["Seq"].ToString();
                        DMTree.TenDanhMuc = dtParent.Rows[i]["TenDanhMuc"].ToString();
                        DMTree.ParentID = dtParent.Rows[i]["ParentID"].ToString();
                        DMTree.Domain = dtParent.Rows[i]["Domain"].ToString();
                        var Prams = setParam(dt.Rows[0]["Domain"].ToString(), dtParent.Rows[i]["DanhMucSanPhamID"].ToString());
                        DataTable dts = DataProvider.SelectStoreProcedure("DM_SanPhamUser_Lst", Prams);
                        if (dts.Rows.Count > 0)
                        {
                            List<ProductCatalog> DMSPList = new List<ProductCatalog>();
                            for (int j = 0; j < dts.Rows.Count; j++)
                            {
                                ProductCatalog DMSP = new ProductCatalog();
                                DMSP.DanhMucSanPhamID = dts.Rows[j]["DanhMucSanPhamID"].ToString();
                                DMSP.Seq = dts.Rows[j]["Seq"].ToString();
                                DMSP.TenDanhMuc = dts.Rows[j]["TenDanhMuc"].ToString();
                                DMSP.ParentID = dts.Rows[j]["ParentID"].ToString();
                                DMSP.Domain = dts.Rows[j]["Domain"].ToString();
                                DMSPList.Add(DMSP);
                            }
                            DMTree.ListTree = DMSPList;
                        }
                        else
                        {
                            List<ProductCatalog> DMSPList = new List<ProductCatalog>();
                            DMTree.ListTree = DMSPList;
                        }
                        DMTreeList.Add(DMTree);
                    }
                }
            }
            return View(DMTreeList);
        }

        public List<SqlParameter> setParam(string domain, string parentID)
        {
            List<SqlParameter> Pram = new List<SqlParameter>();
            SqlParameter ParentID = new SqlParameter("@ParentID", SqlDbType.Int);
            ParentID.Value = parentID;
            Pram.Add(ParentID);
            SqlParameter Domain = new SqlParameter("@Domain", SqlDbType.VarChar);
            Domain.Value = domain;
            Pram.Add(Domain);
            return Pram;
        }

    }
}
